<?php

namespace app\shopapi\controller;


use app\admin\model\Shop;
use app\api\model\OrderCreate as OrderCreateModel;

use app\common\controller\Shopapi;
use think\Request;


/**
 * 店铺订单接口
 */
class Ordercreate extends Shopapi
{
    protected $noNeedRight = '*';

    protected $shop_id;

    protected $shop_name;

    protected $shop_info;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        /*$shop_info = \app\admin\model\Shop::alias('s')
            ->join('shop_category sc', 'sc.id=s.category_id', 'LEFT')
            ->where(['s.user_id'=>$this->auth->id])
            ->field('s.*,sc.name as category_name')->find();

        if (empty($shop_info)){
            $this->error('当前用户不是店铺管理员');
        }*/
        $shop_info = Shop::get(['username'=>$this->shopauth->username]);
        if (empty($shop_info)){
            $this->error('当前用户不是店铺管理员');
        }
        $this->shop_info = $shop_info;
        $this->shop_id = $shop_info->id;
        $this->shop_name = $shop_info->shop_name;
    }


    //提交订单
    public function create()
    {
        try {
            $model = new OrderCreateModel();

            $data = [
                'user_id'=>input('user_id',0),
                'shop_id'=>$this->shop_id,
                'money'=>input('money',0),
                'user_coupon_id'=>input('user_coupon_id',0),
                'create_type'=>1
            ];
            if (empty($data['user_id'])){
                $this->error('缺少参数用户id');
            }
            if (empty($data['money'])){
                $this->error('缺少参数付款金额');
            }
            $res = $model->ordercreate($data);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //订单计算
    public function calculate()
    {
        try {
            $model = new OrderCreateModel();

            $data = [
                'user_id'=>input('user_id',0),
                'shop_id'=>$this->shop_id,
                'money'=>input('money',0),
                'user_coupon_id'=>input('user_coupon_id',0),
                'create_type'=>1
            ];
            if (empty($data['shop_id'])){
                $this->error('缺少参数店铺id');
            }
            if (empty($data['money'])){
                $this->error('缺少参数付款金额');
            }
            $res = $model->calculate($data);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('',$res);
    }
}